package com.itgaohe.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itgaohe.dao.entity.SeckillVoucher;
import com.itgaohe.dao.entity.Voucher;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface VoucherMapper extends BaseMapper<Voucher> {
    //查询有效优惠券
    @Select("SELECT * FROM tb_voucher WHERE id = #{id} AND status = 1")
    Voucher selectAvailableVoucherById(Long id);

    // 查询秒杀券信息
    @Select("SELECT * FROM tb_seckill_voucher WHERE voucher_id = #{voucherId}")
    SeckillVoucher selectForUpdate(Long voucherId);

    // 扣减库存
    @Update("UPDATE tb_seckill_voucher SET stock = stock - 1, update_time = NOW() " +
            "WHERE voucher_id = #{voucherId} AND stock > 0")
    int deductStock(Long voucherId);

}
